package com.example;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.*;






/**
 * Created by IntelliJ IDEA.
 * User: Andrzej
 * Date: 29.04.11
 * Time: 15:37
 * To change this template use File | Settings | File Templates.
 */
public class ViewList extends  Activity{

    private DataHelper dh;
     private Button ADD;
     private Button BACK;
     private TextView day;
     private ArrayList<Task> searchResults;
     ListView lv1;
     MyCustomBaseAdapter ad;

     public void onCreate(Bundle savedInstanceState)
			{
                super.onCreate(savedInstanceState);
			    setContentView(R.layout.listlayout);

                this.ADD=(Button)this.findViewById(R.id.task_add);
                this.BACK=(Button)this.findViewById(R.id.BackToCalendar);
                this.day=(TextView) this.findViewById(R.id.currentday);

                day.setText("Selected day: " + (String) getIntent().getExtras().get("date"));
                searchResults = GetSearchResults();
                  lv1 = (ListView) findViewById(R.id.ListView01);
                  ad=  new MyCustomBaseAdapter(ViewList.this, searchResults);
                        lv1.setAdapter(ad);




                this.BACK.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        finish();
                                }
                         });

                 this.ADD.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                          Intent in = new Intent(ViewList.this, AddToList.class);
                          in.putExtra("date",(String) getIntent().getExtras().get("date"));
                          ViewList.this.startActivityForResult(in,0);



                                }
                         });


            }

    @Override
            protected void onActivityResult(int requestCode,
                                     int resultCode, Intent data) {
    	    super.onActivityResult(requestCode, resultCode, data);

    	    Task temp=(Task)data.getExtras().get("result");
            searchResults.add(temp);
            ((MyCustomBaseAdapter) ad).notifyDataSetChanged();

                       lv1.refreshDrawableState();



    };

    private ArrayList<Task> GetSearchResults(){
     ArrayList<Task> results = new ArrayList<Task>();
      this.dh = new DataHelper(this);
     Task sr1 = new Task("","","","","","");
     results.add(sr1);
     ArrayList<Task> listaa = dh.selectDate((String) getIntent().getExtras().get("date"));



     return listaa;
    }

}
